from django.db import models
from file.models import FileInfo


# Create your models here.
class PdfInfo(models.Model):
    pOrder = models.PositiveIntegerField(null=True, blank=True, verbose_name='ID', editable=False)
    sForeign = models.ForeignKey(FileInfo, on_delete=models.CASCADE, default=0)
    pName = models.CharField(max_length=20, help_text="名称", db_index=True, verbose_name="名称")
    pLike = models.IntegerField(help_text="点赞次数", db_index=True, verbose_name="点赞次数", default=0, editable=False)
    pCollect = models.IntegerField(help_text="收藏次数", db_index=True, verbose_name="收藏次数", default=0, editable=False)

    class Meta:
        db_table = "pdf_info"

    def __str__(self):
        return self.pName

    def save(self, *args, **kwargs):
        # 在保存时更新 display_order
        if not self.pOrder:
            max_order = PdfInfo.objects.aggregate(max_pOrder=models.Max('pOrder'))['max_pOrder']
            self.pOrder = (max_order or 0) + 1
        super().save(*args, **kwargs)
